<?php

/**
 * sfApply actions.
 *
 * @package    5seven5
 * @subpackage sfApply
 * @author     Tom Boutell, tom@punkave.com
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */

require_once dirname(__FILE__).'/../lib/BasesfApplyActions.class.php';
class sfApplyActions extends BasesfApplyActions
{
    //When user is applying for new account
  public function executeApply(sfRequest $request)
  {
    //If user is logged in, we're forwarding him to settings page from apply
    $this->forwardIf($this->getUser()->isAuthenticated(), 'sfApply', 'settings');

    // we're getting default or customized applyForm for the task
    if( !( ($this->form = $this->newForm( 'applyForm' ) ) instanceof sfGuardUserProfileForm) )
    {
      // if the form isn't instance of sfApplyApplyForm, we don't accept it
      throw new InvalidArgumentException(
          'The custom apply form should be instance of sfApplyApplyForm' );
    }

    //Code below is used when user is sending his application!
    if( $request->isMethod('post') )
    {
      //gathering form request in one array
      $formValues = $request->getParameter( $this->form->getName() );
      
        //binding request form parameters with form
      $this->form->bind( $formValues, $request->getFiles( $this->form->getName() ) );
      if ($this->form->isValid())
      {
        $guid = "n" . self::createGuid();
        $this->form->getObject()->setValidate( $guid );
        $date = new DateTime();
        $this->form->getObject()->setValidateAt( $date->format( 'Y-m-d H:i:s' ) );
        $this->form->getObject()->setSkin(1);
		$this->form->save();
        $confirmation = sfConfig::get( 'app_sfForkedApply_confirmation' );
        if( $confirmation['apply'] )
        {
          try
          {
            //Extracting object and sending creating verification mail
            $profile = $this->form->getObject();
            $this->sendVerificationMail($profile);
            return 'After';
          }
          catch (Exception $e)
          {
            //Cleaning after possible exception thrown in ::sendVerificationMail() method
            $profile = $this->form->getObject();
            $user = $profile->getUser();
            $user->delete();
            //We rethrow exception for the dev environment. This catch
            //catches other than mailer exception, i18n as well. So developer
            //now knows what he's up to.
            if( sfContext::getInstance()->getConfiguration()->getEnvironment() === 'dev' )
            {
              throw $e;
            }
            return 'MailerError';
          }
        }
        else
        {
          
		  //Ajout des droits
			$group = new sfGuardUserGroup();
			$group->setUserId($this->form->getObject()->getSfGuardUser()->getId());
			$group->setGroupId(1);
			$group->setCreatedAt(date("Ymdhis"));
			$group->setUpdatedAt(date("Ymdhis"));
			$group->save();
			if( sfContext::getInstance()->getConfiguration()->getEnvironment() != 'dev' )
            {
				$message = $this->getMailer()->compose(
					  array('contact@explications-informatiques.fr' => "L'équipe d'explications-informatiques.fr"),
					  $this->form->getObject()->getSfGuardUser()->getEmailAddress(),
					  'Votre inscription sur Explications Informatiques',
						"
						Vous avez créé un compte sur explications-informatiques et nous vous en remerçions!
						
						Nous espérons vous recevoir et répondre à vos questions informatiques!
						Vous pouvez aussi participer à la rédactions des articles et du lexique.
						
						A trés bientôt sur http://explications-informatiques.fr! 
						"
					);
	 
				$this->getMailer()->send($message);
			}
			$this->activateUser( $this->form->getObject()->getSfGuardUser() );
          return $this->redirect( 'compte/selectlevel' );
        }
		
		
		
		
      }
    }
  }
}